package tianwang.biz;

import java.sql.SQLException;
import java.util.List;

import tianwang.dao.IBatisDao;
import tianwang.model.CAFile;
import tianwang.model.CAStyle;
import tianwang.services.collect.CollectResult;

/**
 * 与style表相关的处理
 * 
 * 对处理完的内容进行识别与保存的处理
 * 
 * 依据要更新的内容，可能随时修改累的内容
 * 
 * @author tian
 *
 */
public class CAStyleGenerateAndSave {
	private IBatisDao dao = null;
	private CAFile caFile;//需要处理的文件
	
	public CAStyleGenerateAndSave(IBatisDao dao){
		this.dao = dao;
	}
	public void setCaFile(CAFile caFile) {
		this.caFile = caFile;
	}
	
	public void generateAndSave(List<CollectResult> list) throws SQLException{
		CAStyle caStyle = this.dao.getCAStyleFromCAFile(caFile);
		if(caStyle.getId() == null){
			caStyle.setFile_id(caFile.getId());
		}
		/*
		 * 将计算结果
		 */
		String collecterName = null;
		for(CollectResult aCollectRe: list){
			//当文件没有内容时，返回结果为null
			if(aCollectRe == null){
				return;
			}
			
			collecterName = aCollectRe.getName();
			//这里可以使用反射机制
			if(collecterName.equals(CAStyle.SHORT_SENTENCE_NUM)){
				caStyle.setShortSentence((Integer) aCollectRe.pump());
			}else if(collecterName.equals(CAStyle.LONG_SENTENCE_NUM)){
				caStyle.setLongSentence((Integer) aCollectRe.pump());
			}else if(collecterName.equals(CAStyle.STANDARD_DEVIATION_SENTENCE_LENGTH)){
				caStyle.setSdSentenceLength((Integer) aCollectRe.pump());
			}else if(collecterName.equals(CAStyle.AVERAGE_SENTENCE_LENGTH)){
				caStyle.setAvgSentenceLength((Integer) aCollectRe.pump());
			}
		}
		
		//判断：如果caFile的id不为空，则更新
		this.dao.insertStyleOrUpdate(caStyle);
	}

	
}
